﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CocisDBUtility;
using System.Data;
using System.Data.SqlClient;
namespace CocisDAL
{
    public class LogsDAL
    {
        #region 插入日志信息
        /// <summary>
        /// 插入日志信息
        /// </summary>
        /// <param name="events"></param>
        /// <param name="user"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public int AddNewLogsInfo(string events, string user, string ip)
        {
            int temp = 0;
            string sql = "insert into logsInfo(events,LoginUser,ip) values('" + events + "','" + user + "','" + ip + "') ";
            temp = SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);
            return temp;
        }
        #endregion

        #region 删除日志信息
        /// <summary>
        /// 删除日志信息
        /// </summary>
        /// <param name="events"></param>
        /// <param name="user"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public int DeleteLogsInfo(int id)
        {
            int temp = 0;
            string sql = "delete from  logsInfo where Logid='"+id+"' ";
            temp = SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);
            return temp;
        }
        #endregion


        #region 查询日志信息
        /// <summary>
        /// 插入日志信息
        /// </summary>
        /// <param name="events"></param>
        /// <param name="user"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public DataSet GetLogsInfo(string user,int type,DateTime start,DateTime end)
        {
            //convert(varchar(20),date,111)
            string sql = "select * from  logsInfo where convert(varchar(100),date,23) between @start and  @end ";
            if (user != "")
            {
                sql += " and Loginuser='" + user + "'";
            }
            if (type == 1)//登录日志
            {
                
                sql += " and events='system login'";
 
            }
            if (type == 2)//操作日志
            {

                sql += " and events<>'system login'";

            }

            sql += " order by Logid desc ";
            SqlParameter[] param = {
                                       new SqlParameter("@start",start),
                                       new SqlParameter("@end",end)
                                   };
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr,CommandType.Text,sql,param);
        }
        #endregion


    }
}
